<?php

/**
 * Config.php
 * 
 * 配置信息管理类，本类是用于项目的配置文件的载入和使用的
 ×
 * @author wang <wang@fvzone.com>
 * @copyright  Copyright (c) 2010 Fire vision Software Studio
 * @link http://www.fvzone.com
 * @license New BSD License.{@link http://www.opensource.org/licenses/bsd-license.php}
 * @version $Id: Config.php 1.1 2011-8-12 22:48:01Z wang $
 * @package core
 * @since 1.0
 */

defined('IN_WORK') or exit('Authorization error');

class Config extends Work
{
    /**
     * 配置数据
     * @var array
     */
    public static $config = array();

    /**
     * 设置配置数据，支持数组
     */
    public static function set($key, $value = null)
    {
        if(is_array($key))
        {
            foreach ($key as $k => $v)
            {
                self::$config[$k] = $v;
            }
        }
        else if(is_string($key)) 
        {
            self::$config[$key] = $value;
        }
    }

    public static function get($key, $default = null)
    {
        if(isset(self::$config[$key]))
        {
            return self::$config[$key];
        }
        return $default;
    }
    
    /**
     * Work::config()->foo = 'something';
     */
    public function __set($key, $value)
    {
        return $this->set($key, $value);
    }

    /**
     * $foo = Work::config()->foo;
     */
    public function __get($key)
    {
        return $this->get($key);
    }

    /**
     * 静态加载项目设置目录(config目录)中的配置文件
     *
     * @param string $name    所要加载的配置文件名 注：不含后缀名
     * @return mixed          配置文件内容
     */
    public function load($name)
    {
        if (!isset(self::$config[$name]))
        {
            //当前配置文件名
            $filename = 'config' .DS. $name.'.php';

            //加载当前组件目录的配置文件
            $file = COMPONENT_DIR. Request::component() .DS. $filename;

            //加载应用配置文件
            if (!is_file($file))
            {
                $file = PUBLIC_DIR.$filename;
            }

            //判断文件是否存在
            if (!is_file($file))
            {
                throwException('The config file:' . $name . '.php Not found!');
            }

            self::$config[$name] = include $file;
        }
        return self::$config[$name];
    }
}
